package com.ZeroCarbon.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ZeroCarbon.domain.dto.IdentityDetailDTO;
import com.ZeroCarbon.domain.dto.IdentityPreviewDTO;
import com.ZeroCarbon.domain.dto.IdentityReceiverDTO;
import com.ZeroCarbon.domain.po.Identity;
import com.ZeroCarbon.exception.BusinessLogicException;
import com.ZeroCarbon.exception.FileHandingException;
import com.ZeroCarbon.mapper.IdentityMapper;
import com.ZeroCarbon.service.IIdentityService;
import com.ZeroCarbon.utils.UserHolder;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import static com.ZeroCarbon.exception.BusinessLogicException.ERROR_EXCEPTION;

@Service
@RequiredArgsConstructor
public class IdentityServiceImpl extends ServiceImpl<IdentityMapper, Identity> implements IIdentityService {

    /**
     * 查询当前用户所拥有店铺的数量
     */
    @Override
    public long getIdentitiesNumber() throws BusinessLogicException {
        //获取用户id
        String userId = UserHolder.getUser();
        //用户身份构建器
        LambdaQueryWrapper<Identity> userIdentitiesWrapper = new LambdaQueryWrapper<Identity>()
                .eq(Identity::getUserId, userId);
        try {
            //返回数量
            return count(userIdentitiesWrapper);
        } catch (Exception e) {
            throw new BusinessLogicException(ERROR_EXCEPTION);
        }
    }

    /**
     * 添加商家
     */
    @Override
    public void addIdentity(IdentityReceiverDTO identityReceiverDTO) throws FileHandingException, BusinessLogicException {
        //获取商家数量
        long number = getIdentitiesNumber();
        //如果商家数量大于10则不允许被添加
        if (number >= 10) {
            throw new BusinessLogicException("最多同时存在10个商家身份");
        }
        //转化对象
        Identity identity = identityReceiverDTO.toIdentity();
        try {
            //保存信息
            if (!save(identity)) {
                throw new BusinessLogicException();
            }
        } catch (Exception e) {
            throw new BusinessLogicException(ERROR_EXCEPTION);
        }
    }

    /**
     * 获取当前用户所有注册的商家身份
     */
    @Override
    public List<IdentityDetailDTO> getIdentities() {
        //获取用户id
        String userId = UserHolder.getUser();
        //用户身份构造器
        LambdaQueryWrapper<Identity> userIdentitiesWrapper = new LambdaQueryWrapper<Identity>()
                .eq(Identity::getUserId, userId);
        //获取用户商店列表
        List<Identity> identities = list(userIdentitiesWrapper);
        //用户没有注册商店
        if (identities == null) {
            return Collections.emptyList();
        }
        //获取注册的商店信息
        return identities.stream()
                .map(Identity::toIdentityDetailDTO)
                .collect(Collectors.toList());
    }

    /**
     * 获取身份简略信息
     */
    @Override
    public List<IdentityPreviewDTO> getIdentitiesPreview() {
        return BeanUtil.copyToList(getIdentities(), IdentityPreviewDTO.class);
    }

    /**
     * 根据id获取商店
     */
    @Override
    public IdentityDetailDTO getIdentity(String id) throws BusinessLogicException {
        Identity identity;
        try {
            //根据id获取商店信息
            identity = getById(id);
        } catch (Exception e) {
            throw new BusinessLogicException(ERROR_EXCEPTION);
        }
        //返回信息
        return identity.toIdentityDetailDTO();
    }

    /**
     * 更新商店信息
     *
     * @param identityReceiverDTO 具体商店信息
     */
    @Override
    public void updateIdentity(IdentityReceiverDTO identityReceiverDTO) throws BusinessLogicException {
        //身份更新构造器
        LambdaQueryWrapper<Identity> oldIdentityWrapper = new LambdaQueryWrapper<Identity>()
                .eq(Identity::getId, identityReceiverDTO.getId());
        try {
            //将接收到的identity新信息更新到数据库
            boolean updateIdentity = update(identityReceiverDTO.toIdentity(), oldIdentityWrapper);
            //更新失败的情况
            if (!updateIdentity) {
                throw new BusinessLogicException();
            }
        } catch (Exception e) {
            throw new BusinessLogicException(ERROR_EXCEPTION);
        }
    }

    /**
     * 删除商家
     */
    @Override
    public void deleteIdentity(String id) throws BusinessLogicException {
        try {
            //根据id移除信息
            if (!removeById(id)) {
                throw new BusinessLogicException();
            }
        } catch (Exception e) {
            throw new BusinessLogicException(ERROR_EXCEPTION);
        }
    }
}
